import { defineStore } from 'pinia'
import { ref } from 'vue'
import { studentApi } from '@/api'

export const useStudentStore = defineStore('student', () => {
  // 状态
  const students = ref([])
  const total = ref(0)
  const loading = ref(false)
  const searchForm = ref({
    sno: '',
    sname: '',
    sex: '',
    dept: '',
    minAge: null,
    maxAge: null
  })
  const pagination = ref({
    page: 1,
    size: 10
  })

  // 获取学生列表
  const fetchStudents = async (params = {}) => {
    loading.value = true
    try {
      // 映射前端字段名到后端字段名
      const queryParams = {
        sno: searchForm.value.sno,
        sname: searchForm.value.sname,
        ssex: searchForm.value.sex,  // 前端sex -> 后端ssex
        sdept: searchForm.value.dept, // 前端dept -> 后端sdept
        minAge: searchForm.value.minAge,
        maxAge: searchForm.value.maxAge,
        pageNum: pagination.value.page,
        pageSize: pagination.value.size,
        ...params
      }
      
      const response = await studentApi.queryStudents(queryParams)
      if (response.code === 200) {
        students.value = response.data.records || []
        total.value = response.data.total || 0
        return true
      }
      return false
    } catch (error) {
      console.error('获取学生列表错误:', error)
      return false
    } finally {
      loading.value = false
    }
  }

  // 添加学生
  const createStudent = async (studentData) => {
    try {
      const response = await studentApi.addStudent(studentData)
      if (response.code === 200) {
        await fetchStudents() // 刷新列表
        return { success: true, message: '添加学生成功' }
      }
      return { success: false, message: response.message || '添加学生失败' }
    } catch (error) {
      console.error('添加学生错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 更新学生
  const modifyStudent = async (studentId, studentData) => {
    try {
      const response = await studentApi.updateStudent({ ...studentData, sno: studentId })
      if (response.code === 200) {
        await fetchStudents() // 刷新列表
        return { success: true, message: '更新学生成功' }
      }
      return { success: false, message: response.message || '更新学生失败' }
    } catch (error) {
      console.error('更新学生错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 删除学生
  const removeStudent = async (studentId) => {
    try {
      const response = await studentApi.deleteStudent(studentId)
      if (response.code === 200) {
        await fetchStudents() // 刷新列表
        return { success: true, message: '删除学生成功' }
      }
      return { success: false, message: response.message || '删除学生失败' }
    } catch (error) {
      console.error('删除学生错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 批量导入学生
  const batchImportStudents = async (studentsData) => {
    try {
      const response = await studentApi.batchImportStudents(studentsData)
      if (response.code === 200) {
        await fetchStudents() // 刷新列表
        return { success: true, message: '批量导入学生成功' }
      }
      return { success: false, message: response.message || '批量导入学生失败' }
    } catch (error) {
      console.error('批量导入学生错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 重置搜索条件
  const resetSearch = () => {
    searchForm.value = {
      sno: '',
      sname: '',
      sex: '',
      dept: '',
      minAge: null,
      maxAge: null
    }
    pagination.value.page = 1
  }

  // 设置分页
  const setPagination = (page, size) => {
    pagination.value.page = page
    pagination.value.size = size
  }

  return {
    // 状态
    students,
    total,
    loading,
    searchForm,
    pagination,
    
    // 方法
    fetchStudents,
    createStudent,
    modifyStudent,
    removeStudent,
    batchImportStudents,
    resetSearch,
    setPagination
  }
})